package com.study.qqclient.service;

import com.study.qqcommon.Message;
import com.study.qqcommon.MessageType;
import com.study.qqcommon.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;

//用户注册登录等功能
public class UserClientService {
   //可能在其他地方使用user对
    private User u = new User();
    private Socket socket;

    public  boolean checkUser(String userId, String password) {
        u.setUserId(userId);
        u.setPassword(password);
        try {
            boolean flag = false;
            socket =  new Socket(InetAddress.getByName("127.0.0.1"), 9999);
            //发送用户信息
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(u);
            objectOutputStream.flush(); // 确保消息立即发送

            //读取服务器返回的信息
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) ois.readObject();
            if(message.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)) {
                //创建一个和服务器通信的线程，创建一个线程类 ClientConnectServerThread
                ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket);
                clientConnectServerThread.start();
                ManageClientContentServiceThread.addClientConnectServerThread(userId, clientConnectServerThread);
                flag = true;
            } else {
                socket.close();
            }
            return flag;
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //请求在线用户列表
    public void getOnlineUser() {
        //发送一个MessageType 为 MESSAGE_GET_ONLINE_FRIEND 的消息
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_FRIEND);
        message.setSender(u.getUserId());
        //发送给服务器
        try {
            //得到对应用户的线程 ClientConnectServerThread
            ObjectOutputStream oos = ManageClientContentServiceThread.getClientConnectServerThread(u.getUserId()).getOos();
            oos.writeObject(message);
            oos.flush();

        } catch (IOException  e) {
            throw new RuntimeException(e);
        }
    }

    //退出线程
    public void exit(){
        //发送一个MessageType 为 MESSAGE_CLIENT_EXIT 的消息
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
        message.setSender(u.getUserId());
        //发送给服务器
        try {
            ObjectOutputStream oos = ManageClientContentServiceThread.getClientConnectServerThread(u.getUserId()).getOos();
            oos.writeObject(message);
            oos.flush();
            ManageClientContentServiceThread.removeClientConnectServerThread(u.getUserId());
            System.out.println("User " + u.getUserId() + " exit");
            System.exit(0);
        } catch (IOException  e) {
            throw new RuntimeException(e);
        }
    }


}
